<?php 
class ControllerFeedHeurekaCz extends Controller {
	public function index() {
		if ($this->config->get('heureka_cz_status')) { 
			$output  = '<?xml version="1.0" encoding="UTF-8" ?>';
      $output .= '<SHOP>';
			
			$this->load->model('catalog/category');
			
			$this->load->model('catalog/product');
			
			$this->load->model('tool/image');
			
			$products = $this->model_catalog_product->getProducts();
			
			foreach ($products as $product) {
				if ($product['description']) {
					$output .= '<SHOPITEM>';
					$output .= '<ITEM_ID>' . $product['product_id'] . '</ITEM_ID>';
					$output .= '<PRODUCTNAME>' . html_entity_decode($product['manufacturer'] . ' ' . $product['name']) . '</PRODUCTNAME>';
					$output .= '<PRODUCT>' . html_entity_decode($product['manufacturer'] . ' ' . $product['name']) . '</PRODUCT>';
					$output .= '<DESCRIPTION>' . $product['description'] . '</DESCRIPTION>';
					$output .= '<URL>' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . '</URL>';
					
					if ($product['image']) {
						$output .= '<IMGURL>' . $this->model_tool_image->resize($product['image'], 500, 500) . '</IMGURL>';
					}
					
          $currency = 'CZK';
					if ((float)$product['special']) {
              $output .= '<PRICE_VAT>' .  $this->currency->format($this->tax->calculate($product['special'], $product['tax_class_id']), $currency, false, false) . '</PRICE_VAT>';
          } else {
              $output .= '<PRICE_VAT>' . $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id']), $currency, false, false) . '</PRICE_VAT>';
          }
			   
					$output .= '<ITEM_TYPE>new</ITEM_TYPE>';

          // <PARAM>
				  $options = $this->model_catalog_product->getProductOptions($product['product_id']);
				  if ($options) {
  				  foreach ($options as $option) {
                $output .= '<PARAM>';
                $output .= '<PARAM_NAME>' . $option['name'] . '</PARAM_NAME>';
                if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
                  foreach($option['option_value'] as $option_value) {
                      if ($option_value['quantity']) {
                          $output .= '<VAL>' . $option_value['name'] . '</VAL>';
                      }
                  }
                } else {
                    $output .= '<VAL>' . $option['option_value'] . '</VAL>';
                }
                $output .= '</PARAM>';
            }
          }
          
					$output .= '<MANUFACTURER>' . html_entity_decode($product['manufacturer'], ENT_QUOTES, 'UTF-8') . '</MANUFACTURER>';

					$categories = $this->model_catalog_product->getCategories($product['product_id']);
					if ($categories) {
  					foreach ($categories as $category) {
  						$path = $this->getPath($category['category_id']);
  						
  						if ($path) {
  							$string = '';
  							
  							foreach (explode('_', $path) as $path_id) {
  								$category_info = $this->model_catalog_category->getCategory($path_id);
  								
  								if ($category_info) {
  									if (!$string) {
  										$string = $category_info['name'];
  									} else {
  										$string .= ' &gt; ' . $category_info['name'];
  									}
  								}
  							}
  						 
  							$output .= '<CATEGORYTEXT>' . $string . '</CATEGORYTEXT>';
  						}
  					}
					}
					
					if ($product['upc']) {
              $output .= '<EAN>' . $product['upc'] . '</EAN>';
          }
          if ($product['quantity']) {
              $output .= '<DELIVERY_DATE>' . '0' . '</DELIVERY_DATE>';
          } 
					$output .= '</SHOPITEM>';
				}
			}
			
			$output .= '</SHOP>'; 
			
			$this->response->addHeader('Content-Type: application/rss+xml');
			$this->response->setOutput($output);
		}
	}
	
	protected function getPath($parent_id, $current_path = '') {
		$category_info = $this->model_catalog_category->getCategory($parent_id);
	
		if ($category_info) {
			if (!$current_path) {
				$new_path = $category_info['category_id'];
			} else {
				$new_path = $category_info['category_id'] . '_' . $current_path;
			}	
		
			$path = $this->getPath($category_info['parent_id'], $new_path);
					
			if ($path) {
				return $path;
			} else {
				return $new_path;
			}
		}
	}		
}
?>